<?php
// +----------------------------------------------------------------------
// Csmmeet [ Csmonline ]
// Author: chensm <chenshiming0802@163.com>
// Create by chensm at 2020-03-31
// +----------------------------------------------------------------------
namespace app\admin\controller\csmonline;

use addons\csmonline\library\CsmBackend;
use addons\csmonline\library\CsmonlineCron;

/**
 * Tables
 * http://127.0.0.1/fastadmin_plugin_csmmeet/public/q3HJDu2RgE.php/csmonline/chartregisterhour/index
 */
class Chartregisterhour extends CsmBackend
{
    protected $model = null;

    protected $relationSearch = true;

    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $begin = $this->csmreq("begin", true);
            $end = $this->csmreq("end", true);

            $today = date("Y-m-d");
            CsmonlineCron::crononedate($today, true);

            // $begindate = strtotime($begin);
            // $enddate = strtotime($end);
            $allregisteredus = [];
            $newregisteredus = [];
//             $dau = [];
            for ($i = 0; $i <= 23; $i ++) {
                $dd = $i;
                $allregisteredus[$this->getWeekend($dd)] = [
                    $this->getWeekend($dd),
                    0,
                    0
                ];
                $newregisteredus[$this->getWeekend($dd)] = [
                    $this->getWeekend($dd),
                    0,
                    0
                ];
//                 $dau[$this->getWeekend($dd)] = [
//                     $this->getWeekend($dd),
//                     0,
//                     0
//                 ];
                // $begindate = $begindate + 3600 * 24;
            }

            $dao = new \app\admin\model\csmonline\Piestathour();
            $list = $dao->where("statdate", ">=", $begin)
                ->where("statdate", "<=", $end)
                ->group("stathour")
                ->field("stathour statdate,max(allregisteredus) allregisteredus,sum(newregisteredus) newregisteredus,sum(dau) dau")
                ->order("stathour", "asc")
                ->select();
            // $this->tracedao($dao);
            foreach ($list as $v) {
                $v->statdate = $this->getWeekend($v->statdate);

                $allregisteredus[$v->statdate][1] = $v->allregisteredus;
                $newregisteredus[$v->statdate][1] = $v->newregisteredus;
                // $dau[$v->statdate][1] = $v->dau;
            }

            $result = array(
                // bootstraptable返回的数据
                "code" => "1",
                "total" => count($list),
                "rows" => $list,
                // Echart通过fastadmin调用数据
                'code' => 1,
                'msg' => "",
                "data" => [
                    "allregisteredus" => $this->adjust($allregisteredus),
                    "newregisteredus" => $this->adjust($newregisteredus)
                    // "dau" => $this->adjust($dau)
                ]
            );

            // echo $dao->getLastSql();
            // var_dump($result);die();
            return json($result);
        }
        return $this->view->fetch();
    }

    private function getWeekend($s)
    {
        return $s . "点";
    }

    private function adjust($list)
    {
        $ll = [];
        foreach ($list as $v) {
            $ll[] = $v;
        }
        array_unshift($ll, [
            'product',
            '统计'
        ]);
        return $ll;
    }
}

